package pixeleditor.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JPanel;

import common.core.Tile;
import common.gui.IViewable;
import common.util.FileHandler;

import pixeleditor.core.Board;
import pixeleditor.ctrl.ControlFactory;

@SuppressWarnings("serial")
public class GridPanel extends JPanel implements IViewable {

	public GridPanel() {
		setBackground(Color.WHITE);
		setPreferredSize(new Dimension(MainFrame.WIDTH_ON_WINDOW,MainFrame.HEIGHT_ON_WINDOW-100));
		setVisible(true);
		setOpaque(false);
				
		ControlFactory.getControl(ControlFactory.GRID_CTRL).addView(this);
		ControlFactory.getControl(ControlFactory.MENU_CTRL).addView(this);
		ControlFactory.getControl(ControlFactory.CHOOSE_BACKGROUND_DIALOG_CTRL).addView(this);
		MouseAdapter ma = (MouseAdapter) ControlFactory.getControl(ControlFactory.GRID_CTRL); 
		addMouseMotionListener(ma);
		addMouseListener(ma);
		
		
	}
	
	public void paint(Graphics g) {
		Tile[][] gr = Board.getInstance().getScenery().getTiles();
		if(Board.getInstance().getBak() != null)
			g.drawImage(Board.getInstance().getBak().getBackground(), 0, 0, null);
		for(int i=0; i<gr.length; i++) {
			for(int j=0; j<gr[0].length; j++) {
				//Draw all tiles, the look depends on color and if it is destructible
				if(gr[i][j] != null) {
					g.setColor(gr[i][j].getColor());
					int x = j * Tile.TILE_SIZE;
					int y = i * Tile.TILE_SIZE;
					
					if(gr[i][j].isDestructible()) {
						g.fill3DRect(x, y, Tile.TILE_SIZE, Tile.TILE_SIZE, true);        	
			        } else {
			        	g.fill3DRect(x, y, Tile.TILE_SIZE, Tile.TILE_SIZE, true);
			        	g.fill3DRect(x+1, y+1, Tile.TILE_SIZE-2, Tile.TILE_SIZE-2, true);
			        	g.fill3DRect(x+2, y+2, Tile.TILE_SIZE-4, Tile.TILE_SIZE-4, true);
			        }
				}
			}
		}
		
		try {
			BufferedImage barrel = FileHandler.getImage(FileHandler.PLAYER, "barrel_3d.png");
			BufferedImage body = FileHandler.getImage(FileHandler.PLAYER, "body_3d.png");
			
			//Draw players like they are drawn in the game
			if(Board.getInstance().getPlayer1() != null) {
				Point p1 = Board.getInstance().getPlayer1();
				g.drawImage(
						barrel, 
						(p1.x*10) + (body.getWidth() / 2) - (barrel.getWidth() / 2), 
						(p1.y*10) - 5, 
						null);
				g.drawImage(body,
						(p1.x*10),
						(p1.y*10) + body.getHeight(),
						null);
			}
			if(Board.getInstance().getPlayer2() != null) {
				Point p2 = Board.getInstance().getPlayer2();
				g.drawImage(
						barrel, 
						(p2.x*10) + (body.getWidth() / 2) - (barrel.getWidth() / 2), 
						(p2.y*10) - 5, 
						null);
				g.drawImage(body,
						(p2.x*10),
						(p2.y*10) + body.getHeight(),
						null);
				
			}
			
		} catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}

	@Override
	public void updateView(Object o) {
		repaint();
	}
}
